package io.plus.user_interface.common.listener;

import drds.common.$;
import io.plus.interactive.AsynchronousSocketChannelContext;
import io.plus.user_interface.common.SessionContext;
import io.plus.user_interface.common.UserConfiguration;
import io.plus.user_interface.common.packet.Client;
import io.plus.user_interface.common.packet.Group;
import io.plus.user_interface.common.packet.User;

import java.util.List;


/**
 * 消息持久化绑定监听器
 */
public class BindListenerImpl implements BindListener {
    protected UserConfiguration userConfiguration;

    public BindListenerImpl(UserConfiguration userConfiguration) {
        this.userConfiguration = userConfiguration;
    }

    public UserConfiguration getUserConfiguration() {
        return userConfiguration;
    }

    public void setUserConfiguration(UserConfiguration userConfiguration) {
        this.userConfiguration = userConfiguration;
    }

    @Override
    public void onAfterGroupBind(AsynchronousSocketChannelContext asynchronousSocketChannelContext, String groupId) throws Exception {

        String userId = asynchronousSocketChannelContext.userId;
        if ($.isNullOrEmpty(groupId) || $.isNullOrEmpty(userId)) {
            return;
        }
        User user = new User();
        user.setId(userId);
        Group group = null;
        List<User> userIdList = group.getUserList();
        if (!userIdList.contains(user)) {
            userIdList.add(user);
        }
        SessionContext sessionContext = (SessionContext) asynchronousSocketChannelContext.getAttribute();
        Client client = sessionContext.getClient();
        if (client == null) {
            return;
        }
        User onlineUser = client.getUser();
        if (onlineUser == null) {
            return;
        }
        List<Group> groupList = onlineUser.getGroups();
        if (groupList == null) {
            return;
        }
        for (Group group2 : groupList) {
            if (groupId.equals(group2.getId())) {
                break;
            }
        }
        groupList.add(group);
    }

    @Override
    public void onAfterGroupUnbind(AsynchronousSocketChannelContext asynchronousSocketChannelContext, String groupId) throws Exception {
        String userId = asynchronousSocketChannelContext.userId;

    }


    @Override
    public void onAfterUserBind(AsynchronousSocketChannelContext asynchronousSocketChannelContext, String userid) throws Exception {

        SessionContext sessionContext = (SessionContext) asynchronousSocketChannelContext.getAttribute();
        Client client = sessionContext.getClient();
        if (client == null) {
            return;
        }
        User onlineUser = client.getUser();
        if (onlineUser != null) {


        }
    }

    @Override
    public void onAfterUserUnbind(AsynchronousSocketChannelContext asynchronousSocketChannelContext, String userId) throws Exception {

    }


}
